<?php
class Data_Model_DangKyKhoaHocMapper extends Data_Model_DataMapperAbstract
{
	protected static $_instance = null;
	// dung de phan trang
	private $_paginator = null;
	
	public function dangkyList()
	{
		$db = $this->getDb();
		$selectDkkh = $db->select()
						->from('tbl_dangkykhoahoc');
		$dkkhs = $db->fetchAll($selectDkkh);
		
		$dkkhArray = array();
		foreach($dkkhs as $dkkh)
		{	
			$khoahocMapper = Data_Model_KhoaHocMapper::getInstance();
			$khoahoc = $khoahocMapper->searchByMaKH($dkkh['MaKhoaHoc']);
			$dkkh['tbl_khoahoc']= $khoahoc;
			
			$hocvienMapper = Data_Model_HocVienMapper::getInstance();
			$hocvien = $hocvienMapper->searchById($dkkh['MaHocVien']);
			$dkkh['tbl_hocvien']= $hocvien;
			
			$dkkhArray[] = $this->_populate($dkkh);
		}
		return $dkkhArray;
	}
	
	// hien thi
	public function _populate($data)
	{
		$obj = new Data_Model_DangKyKhoaHoc();
		$obj->setId($data['MaDangKy']);
		$obj->setMahocvien($data['MaHocVien']);
		$obj->setBuoihoc($data['BuoiHoc']);
		$obj->setCahoc($data['CaHoc']);
		$obj->setMakhoahoc($data['MaKhoaHoc']);
		$obj->setKhoahoc($data['tbl_khoahoc']);
		$obj->setHocvien($data['tbl_hocvien']);
		return $obj;
	}
	protected function _insert(Data_Model_DomainObjectAbstract $obj){
	
		$db = $this->getDb();
		/* $mahocvien = $db->lastInsertId('tbl_hocvien','MaHocVien'); */
		$data = array(
				'MaDangKy' => $obj->getId(),
				'MaHocVien' => $obj->getMahocvien(),
				'BuoiHoc' => $obj->getBuoihoc(),
				'CaHoc' => $obj->getCahoc(),
				'MaKhoaHoc' => $obj->getMakhoahoc()
				);
		$db->insert('tbl_dangkykhoahoc',$data);
	}
	protected function _update(Data_Model_DomainObjectAbstract $obj){
		
		$db = $this->getDb();
		$data = array(
				'MaDangKy' => $obj->getId(),
				'MaHocVien' => $obj->getMahocvien(),
				'BuoiHoc' => $obj->getBuoihoc(),
				'CaHoc' => $obj->getCahoc(),
				'MaKhoaHoc' => $obj->getMakhoahoc()
		);
		$where = $db->quoteInto('MaDangKy = ?',$obj->getId());
		$db->update('tbl_dangkykhoahoc',$data,$where);
	}
	
	public function delete($id)
	{
		$db = $this->getDb();
		$where = $db->quoteInto('MaDangKy = ?',$id);
		$db->delete('tbl_dangkykhoahoc',$where);
	}
	
	// Goi chinh no
	
	public static function getInstance()
	{
		if(null === self::$_instance)
		{
			self::$_instance = new self();				
		}
		return self::$_instance;
	}
}